//
//  TFPopoverManager.m
//  OA
//
//  Created by Guo Rui on 2017/9/26.
//Copyright © 2017年 GR Harry. All rights reserved.
//

#import "TFPopoverManager.h"
#import <MBProgressHUD/MBProgressHUD.h>

float const kTFPopoverViewDismissDuration = 0.8f;

@implementation TFPopoverManager

#pragma mark - public

+ (id)showPopoverViewWithViewType:(TFPopoverType)viewType
                           text:(NSString *)text {
    return [TFPopoverManager showPopoverViewInView:[self popupInView] withViewType:viewType text:text];
}

+ (id)showProgressPopoverViewWithProgress:(float)progress text:(NSString *)text {
    return [TFPopoverManager showProgressPopoverViewInView:[self popupInView] withProgress:progress text:text];
}

+ (id)showProgressPopoverViewInView:(UIView *)view withProgress:(float)progress text:(NSString *)text {
    MBProgressHUD *hud = [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeAnnularDeterminate text:text image:@"" autoDismiss:NO];
    hud.progress = progress;
    return hud;
}

+ (id)showHorizontalProgressPopoverViewInView:(UIView *)view withProgress:(float)progress text:(NSString *)text {
    MBProgressHUD *hud = [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeDeterminateHorizontalBar text:text image:@"" autoDismiss:NO];
    hud.progress = progress;
    return hud;
}

+ (id)showPopoverViewInView:(UIView *)view withViewType:(TFPopoverType)viewType text:(NSString *)text {
    switch (viewType) {
        case TFPopoverTypeToast:
        {
            return [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeText text:text image:@"" autoDismiss:YES];
        }
            break;
        case TFPopoverTypeBottomToast: {
            MBProgressHUD *hud = [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeText text:text image:@"" autoDismiss:YES];
            hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
            return hud;
        }
            break;
        case TFPopoverTypeStatus:
        {
            return [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeIndeterminate text:text image:@"" autoDismiss:NO];
        }
            break;
        case TFPopoverTypeSuccess: {
            return [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeCustomView text:text image:@"popup_success" autoDismiss:YES];
        }
            break;
        case TFPopoverTypeFail: {
            return [TFPopoverManager showMBProgressHUDInView:view withMode:MBProgressHUDModeCustomView text:text image:@"popup_error" autoDismiss:YES];
        }
            break;
        case TFPopoverTypeAlert: {
            UIViewController *rootVC =  [[UIApplication sharedApplication].delegate window].rootViewController;
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"提示", nil) message:text preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [alertVC dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertVC addAction:action];
            [rootVC presentViewController:alertVC animated:YES completion:nil];
            return alertVC;
        }
            break;
            
        default:
            break;
    }
}

+ (MBProgressHUD *)showMBProgressHUDInView:(UIView *)view
                                  withMode:(MBProgressHUDMode)mode
                                      text:(NSString *)text
                                     image:(NSString *)image
                               autoDismiss:(BOOL)autoDismiss {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = mode;
    hud.bezelView.color = [UIColor whiteColor];
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont systemFontOfSize:17];
    if (hud.mode == MBProgressHUDModeCustomView) {
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
    }
    if (autoDismiss) {
        [hud hideAnimated:YES afterDelay:kTFPopoverViewDismissDuration];
    }
    return hud;
}

+ (void)dismissPopoverView {
    [self dismissPopoverViewInView:[self popupInView]];
}

+ (void)dismissPopoverViewInView:(UIView *)view {
    
    NSArray *subViews = view.subviews;
    for (UIView *subView in subViews) {
        if ([subView isKindOfClass:[MBProgressHUD class]]) {
            [(MBProgressHUD *)subView hideAnimated:YES];
        }
    }
}

+ (UIView *)popupInView {
    return [[UIApplication sharedApplication].delegate window];
}

@end
